package com.heba.greenmulu.diary;

import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.heba.Debug;
import com.heba.adapter.diary.DiaryTagPhotoPagerAdapter;
import com.heba.banner.ViewPagerFocusView;
import com.heba.bean.diary.ItemImageTagObject;
import com.heba.bean.diary.ItemUploadPhotoObject;
import com.heba.config.CommonConfig;
import com.heba.event.MessageEvent;
import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 上传图片用户标记页面
 * Created by jason on 2020/2/12.
 */
public class ItemDiaryFriendTagPhotoActivity extends BaseActivity {

    private ViewPager viewPager;
    private ViewPagerFocusView focusView;

    private DiaryTagPhotoPagerAdapter adapter;

    private List<ItemUploadPhotoObject> list;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_friend_tag_photo);
        /**
         * 设置狀態欄顏色
         */
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorTextLogIn)
                .init();
        EventBus.getDefault().register(this);
        initTopbar();
        findViewById();
        initView();
        initViewpager();
    }

    @Override
    protected void findViewById() {
        viewPager = findViewById(R.id.viewPager);
        focusView = findViewById(R.id.viewpger_focusview);
    }

    @Override
    protected void initView() {
        txt_title.setText(getResources().getString(R.string.diary_tag_photo_title));

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            position = bundle.getInt(CommonConfig.POSITION, 0);
            list = (List<ItemUploadPhotoObject>) bundle.getSerializable(CommonConfig.DIARY_IMAGE_LIST);
        }

        /**
         * 用集合迭代器删除 防止ConcurrentModificationException异常抛出
         * 移除默认的上传icon
         */
        Iterator<ItemUploadPhotoObject> iterator = list.iterator();
        while (iterator.hasNext()) {
            ItemUploadPhotoObject object = iterator.next();
            if (object.getImageType() == ItemUploadPhotoObject.SHOW_IMAGE_ICON) {
                iterator.remove();
            }
        }

        /**
         * 标记完成
         */
        txt_right.setOnClickListener(v -> {
            Debug.Log(TAG, "添加标签完成 list数据是 " + list.size());

            /**
             * 用户标记完成 通过EventBus刷新数据
             */
            MessageEvent event = new MessageEvent();
            event.setEvent(CommonConfig.EVENT_UPLOAD_PHOTO_LIST);
            event.setUploadPhotoObjectList(list);
            EventBus.getDefault().post(event);
            finishActivity();
        });
    }

    /**
     * initViewpager
     */
    private void initViewpager() {
        adapter = new DiaryTagPhotoPagerAdapter(this, list);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(9);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                focusView.setCurrentIndex(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        if (!list.isEmpty()) {
            //总的点数
            focusView.setCount(list.size(), R.drawable.icon_diary_slideshow_u, R.drawable.icon_diary_slideshow_s);
            //当前位置
            viewPager.setCurrentItem(position);
            focusView.setCurrentIndex(position);
        }
    }

    /**
     * 接收EventBus
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {

        //接收用户数据并刷新view
        if (event.getEvent().equals(CommonConfig.EVENT_IMAGE_TAG)) {
            int imagePos = event.getPosition();
            ItemUploadPhotoObject photoObject = list.get(imagePos);

            //已保存的标签
            List<ItemImageTagObject> tagObjects = photoObject.getImageTagObjectList();

            //新添加的标签
            ItemImageTagObject tagObject = event.getImageTagObject();
            float scaleX = (float) tagObject.getX() / photoObject.getViewWidth();
            float scaleY = (float) tagObject.getY() / photoObject.getViewHeight();
            tagObject.setScaleX(scaleX);
            tagObject.setScaleY(scaleY);

            /**
             * 保存标签到实体类ItemUploadPhotoObject中
             */
            if (tagObjects == null) {
                List<ItemImageTagObject> tagObjects1 = new ArrayList<>();
                //添加标签
                tagObjects1.add(tagObject);
                photoObject.setImageTagObjectList(tagObjects1);
            } else {
                //判断是否重复标记了用户
                for (ItemImageTagObject object : tagObjects) {
                    if (object.getUserID().equals(tagObject.getUserID()))
                        return;
                }
                //添加标签
                tagObjects.add(tagObject);
                photoObject.setImageTagObjectList(tagObjects);
            }

            adapter.updateData(list);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //反註冊
        EventBus.getDefault().unregister(this);
    }

}
